import 'package:flutter/material.dart';
import 'package:flutter_base/23_pageview/keepAliveIwdget.dart';

void main() {
  runApp(const MaterialApp(
    home: PageviewKeepalive(),
  ));
}

class PageviewKeepalive extends StatefulWidget {
  const PageviewKeepalive({super.key});

  @override
  State<PageviewKeepalive> createState() => _PageviewKeepaliveState();
}

class _PageviewKeepaliveState extends State<PageviewKeepalive> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('pageview keep alive'),
      ),
      body: PageView(
        children: const [
          KeepAliveWidget(child: Page1()),
          KeepAliveWidget(child: Page2())
        ],
      ),
    );
  }
}

class Page1 extends StatefulWidget {
  const Page1({super.key});

  @override
  State<Page1> createState() => _Page1State();
}

class _Page1State extends State<Page1> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    print('page111  init------');
  }

  @override
  Widget build(BuildContext context) {
    return Text('Page1');
  }
}

class Page2 extends StatefulWidget {
  const Page2({super.key});

  @override
  State<Page2> createState() => _Page2State();
}

class _Page2State extends State<Page2> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    print('Page222 init');
  }

  @override
  Widget build(BuildContext context) {
    return Text('Page222');
  }
}
